const gulp = require('gulp')
const rollup = require('rollup')
const browserSync = require('browser-sync')
const babel = require('rollup-plugin-babel')
const runSequence = require('run-sequence')
const del = require('del')

var inputOptions = {
  input: 'src/index.js',
  plugins: [
    babel({
      exclude: 'node_modules/**'
    })
  ]
}
var outputOptions = {
  file: 'dist/test.js',
  format: 'umd',// iife(web) , cjs(node) , umd(web&node)
  name: 'MyLib',
  sourcemap: true
}

gulp.task('build', ['clean'], () => {
  return rollup.rollup(inputOptions).then(bundle => {
    bundle.write(outputOptions)
  })
})
gulp.task('clean', () => del(['.tmp', 'dist/*', '!dist/.git'], {dot: true}))
gulp.task('serve', ['build'], () => {
  return browserSync({
    notify: false,
    logPrefix: 'i',
    // 允许滚动在断点之间同步
    scrollElementMapping: ['main', '.mdl-layout'],
    // https: true,
    server: ['./'],
    port: 3002
  })
})

gulp.task('refresh', () => {
  browserSync.reload()
})
gulp.task('watch', () => {
  gulp.watch(['src/**/*.js'], ['build', 'refresh'])
  gulp.watch(['examples/**/*.js', 'examples/**/*.html'], ['refresh'])
})

gulp.task('default', () => {
  runSequence(['serve', 'watch'])
})
